home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 706 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.5 KB

  1. From: willer@interlog.com (Steve Willer)
  2. Message-ID: <31474cd9.10139718@news.interlog.com>
  3. X-Original-Date: Wed, 13 Mar 1996 23:02:38 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 13 Mar 96 23:51:30 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: auto_ptr: no operator bool()?
  9. Organization: InterLog Internet Services
  10. References: <313ddfd9.16044605@sqarc.sq.com> <4i2jqr$8dl@solutions.solon.com>
  11. X-Newsreader: Forte Agent .99d/32.182
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMUdfkeEDnX0m9pzZAQH2uwF+Icw5iWYz8a+Hpaq0j7UrXEG3euLSNN6/
  14.     jiPMFV3llZbbvfqetuNOE5WM03j+B/6c
  15.     =VgJR
  16.  
  17. James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
  18.  
  19. >The real danger is in the following:
  20. >
  21. >    auto_ptr< T >    p1 ;
  22. >    auto_ptr< T >    p2 ;
  23. >
  24. >    if ( p1 == p2 ) ...
  25.  
  26. Well, there is obviously a solution of either modifying the way bool
  27. works (so it can't be compared to another), or perhaps creating a new
  28. explicit_bool type.
  29.  
  30. But what about this solution as a way to implement an auto_ptr:
  31.  
  32. class explicit_bool {
  33.    bool state_;
  34.    bool operator==(const explicit_bool &rhs) const;
  35.    bool operator!=(const explicit_bool &rhs) const;
  36. public:
  37.    explicit_bool(bool state) { state_ = state; }
  38.    operator bool() { return state_; };
  39. };
  40.  
  41. template <class T> class auto_ptr {
  42. public:
  43.    /* explicit */ auto_ptr(T *p=0): pointee(p) {}
  44. //  template<class U> auto_ptr(auto_ptr<U> &rhs):
  45. pointee(rhs.release()) {}
  46.    auto_ptr(auto_ptr<T> &rhs): pointee(rhs.release()) {}
  47.    ~auto_ptr() { delete pointee; }
  48. //  template<class U> auto_ptr<T>& operator=(auto_ptr<U> &rhs) {
  49.    auto_ptr<T> &operator=(auto_ptr<T> &rhs) {
  50.       if (this != &rhs) reset(rhs.release());
  51.       return *this;
  52.    }
  53.    T& operator*() const {return *pointee;}
  54.    T* operator->() const {return pointee;}
  55.    T* get() const {return pointee;}
  56.    T* release() {
  57.       T *oldPointee = pointee;
  58.       pointee = 0;
  59.       return oldPointee;
  60.    }
  61.    operator explicit_bool() { return explicit_bool(pointee != 0); }
  62.    explicit_bool operator!() { return explicit_bool(pointee == 0); }
  63. //   operator bool() {return (pointee != 0);}  // apparently not part
  64. of the std
  65. //   bool operator!() {return (pointee == 0);} // ditto
  66.    void reset(T *p=0) {delete pointee; pointee = p;}
  67. private:
  68.    T *pointee;
  69. };
  70.  
  71. class myclass{};
  72. int main(char,char**) {
  73.    auto_ptr<myclass> a1,a2;
  74.  
  75.    a1 = a2;
  76.    a1 == a2;
  77.    a1 != a2;
  78.    if (a1);
  79.    if (!a1);
  80.  
  81.    return 0;
  82. }
  83.  
  84. To tell you the truth, I'm not 100% sure this would work properly,
  85. because I don't have my C++ books here (I'm at home sick), and my
  86. compiler doesn't have "bool" as a built-in type (it's a class). With
  87. my current compiler, the "a1==a2" and "a1!=a2" lines don't work
  88. (illegal structure operation), which is exactly how I'd expect it. The
  89. "if (a1)" line doesn't compile either, but wouldn't it work if "bool"
  90. was a built-in type? This would mean an implicit conversion to
  91. explicit_bool and then to bool, but my (somewhat faulty)  memory is
  92. that that's legal.
  93.  
  94. Even if that's not legal, what about defining "template <class T>
  95. template<class U>auto_ptr<T>::operator==(const auto_ptr<U>& rhs)" and
  96. the equivalent operator!= as private?
  97. ---
  98. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  99. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  100. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  101. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  102. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  103.